#coding=utf-8

from utils import *
from common_defines import *
import time

class ActionManager():
    '''Date time Manager'''
    def __init__(self,slm):
        self.SLM=slm
        self.recPath=self.SLM.pydir + '\\stat.xml'
        self.lastAction=self.getCmdStat()
        self.SLM.logger.info('lastAction is ' + str(self.lastAction))

    def isWorking(self):
        '''返回是否在working environment'''
        import os
        if self.SLM.debug == True:
            return True
        else:
            return  self.isInnerIP()  and not self.isNCConnected()

    def isInnerIP(self):
        '''返回是否在内网'''
        import socket
        ip=socket.gethostbyname(socket.gethostname())
##        logger.info('ip is: ' + ip)
        ipList=ip.split('.')
        if ipList[0] == '10':
            return True
        if ipList[0] == '192' and ipList[1] == '168':
            return True
        if ipList[0] == '172':
            ip1=int(ipList[1])
            if ip1 >= 16 and ip1 <= 31:
                return True
        return False

    def isBNACWorking(self):
        '''返回BNAC是否在运行'''
        import os
        if 0 == os.system('tasklist | findstr Netcheck.exe'):
            return True
        else:
            return False

    def isNCConnected(self):
        '''返回是否处于NC模式'''
        import os
        if 0 == os.system('tasklist | findstr dsNetworkConnect.exe'):
            return True
        else:
            return False

    def getCommand(self):
        '''to make command'''
        if self.SLM.debug == False:
            dt=getRealTime('LT')
        else:
            dt=-1
        if dt < 0:
            dt=time.localtime()
            self.SLM.logger.warn('using time.localtime() ' + str(dt))

        cmd=TO_NULL
        if dt[3] == 10 and dt[4] >= 22 and self.lastAction != TO_MED1:
            self.lastAction = TO_MED1
            cmd= TO_MED1
        if dt[3] == 11 and dt[4] >= 36 and self.lastAction != TO_REST1:
            self.lastAction = TO_REST1
            cmd= TO_REST1
        if dt[3] == 14 and dt[4] >= 27 and self.lastAction != TO_REST2:
            self.lastAction = TO_REST2
            cmd=  TO_REST2
        if dt[3] == 15 and dt[4] >= 47 and self.lastAction != TO_REST3:
            self.lastAction = TO_REST3
            cmd=  TO_REST3
        if dt[3] == 16 and dt[4] >= 57 and self.lastAction != TO_MED2:
            self.lastAction = TO_MED2
            cmd=  TO_MED2
        if dt[3] == 19 and dt[4] >= 27 and self.lastAction != TO_REST4:
            self.lastAction = TO_REST4
            cmd=  TO_REST4
            
        if cmd != TO_NULL:
            self.recordCommand(cmd, dt)
        return cmd

    def recordCommand(self, cmd, dt):
        '''set last effective command to a file'''
        f=open(self.recPath,'w')
        f.write(str(time.mktime(dt)) + '\n' + cmd)
        f.close()

    def getCmdStat(self):
        '''get Command stat from the file'''
        cmd=None
        try:
            f=open(self.recPath,'r')
            stat=f.readlines()
            dt=eval(stat[0])
            if time.time() - dt < 3600:
                cmd=stat[1]
        except Exception,e:
            self.SLM.logger.error(str(e))
        finally:
            return cmd


        

if __name__=='__main__':
    pass
